package com.yuke.cloud.service.pmc.service.impl;

import com.yuke.cloud.service.api.opc.dto.UploadFile;
import com.yuke.cloud.service.api.opc.service.OpcFileFeignApi;
import com.yuke.cloud.service.api.pmc.dto.FactoryInfoDto;
import com.yuke.cloud.service.pmc.dao.ProdFactoryMapper;
import com.yuke.cloud.service.pmc.dto.ProdFactoryDto;
import com.yuke.cloud.service.pmc.entity.ProdFactory;
import com.yuke.cloud.service.pmc.service.DicRegionService;
import com.yuke.cloud.service.pmc.service.ProdFactoryService;
import com.yuke.cloud.common.core.support.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


/**
 * Created by zys on 2018/09/28.
 */
@Slf4j
@Service
@Transactional
public class ProdFactoryServiceImpl extends BaseService<ProdFactory> implements ProdFactoryService {
    @Resource
    private ProdFactoryMapper prodFactoryMapper;
    @Resource
    private OpcFileFeignApi opcFileFeignApi;

    @Override
    public int save(ProdFactory record) {
        record.setFactoryId(generateId());
        return super.save(record);
    }

    @Override
    public int update(ProdFactory entity) {
        int count = super.update(entity);
        if (count > 0) {
            List<ProdFactory> prodFactoryList = new ArrayList<>();
            prodFactoryList.add(entity);
            this.deleteImg(prodFactoryList);
        }
        return count;
    }

    private void deleteImg(List<ProdFactory> prodFactories) {
        UploadFile uploadFile = new UploadFile();
        List<String> filePaths = new ArrayList<>();
        for (ProdFactory prodFactory : prodFactories) {
            if (prodFactory.getOldCommitmentImgUri() != null && !"".equals(prodFactory.getOldCommitmentImgUri()))
                filePaths.add(prodFactory.getOldCommitmentImgUri());
            if (prodFactory.getOldFoodPermitImgUri() != null && !"".equals(prodFactory.getOldFoodPermitImgUri()))
                filePaths.add(prodFactory.getOldFoodPermitImgUri());
            if (prodFactory.getOldImgUri() != null && !"".equals(prodFactory.getOldImgUri()))
                filePaths.add(prodFactory.getOldImgUri());
            if (prodFactory.getOldLicenseImgUri() != null && !"".equals(prodFactory.getOldLicenseImgUri()))
                filePaths.add(prodFactory.getOldLicenseImgUri());
            if (prodFactory.getOldLogoUri() != null && !"".equals(prodFactory.getOldLogoUri()))
                filePaths.add(prodFactory.getOldLogoUri());
            if (prodFactory.getOldTobaccoPermitImgUri() != null && !"".equals(prodFactory.getOldTobaccoPermitImgUri()))
                filePaths.add(prodFactory.getOldTobaccoPermitImgUri());
        }
        if (filePaths.size() > 0) {
            uploadFile.setFilePaths(filePaths);
            opcFileFeignApi.filesDelete(uploadFile);
        }
    }

    @Override
    public void deleteByIds(String ids) {
        List<ProdFactory> prodFactoryList = new ArrayList<>();
        super.deleteByIds(ids);
        this.deleteImg(prodFactoryList);
    }

    @Override
    public List<ProdFactoryDto> getListDetailByCondition(ProdFactory prodFactory) {
        List<ProdFactoryDto> prodFactoryDtos = prodFactoryMapper.getListDetailByCondition(prodFactory);
        return prodFactoryDtos;
    }

    @Override
    public List<ProdFactory> getListByCondition(ProdFactory prodFactory) {
        log.info("--- Enter getListByCondition(ProdFactory prodFactory)，参数：{}", prodFactory);
        List<ProdFactory> prodFactoryList = prodFactoryMapper.getListByCondition(prodFactory);
        log.info("--- Leave getListByCondition(ProdFactory prodFactory)，返回值：{}", prodFactoryList);
        return prodFactoryList;
    }

    @Override
    public String getIntroduceBySpecId(Long specId) {
        return prodFactoryMapper.getIntroduceBySpecId(specId);
    }

    @Override
    public List<FactoryInfoDto> getFactoryDetail(List<Long> factoryIds) {
        return prodFactoryMapper.getFactoryDetail(factoryIds);
    }
}
